from dataclasses import field,dataclass
from typing import Any
from flask import jsonify
from .http_code import HttpCode
@dataclass
class Response:
    code: HttpCode.SUCCESS
    message: str = ""
    data: Any = field(default_factory=dict)
def json(data: Response=None):
    return jsonify(data),200

def success(data: Any=None):
    return jsonify(Response(code=HttpCode.SUCCESS, message="", data=data))
def fail(data: Any=None):
    return jsonify(Response(code=HttpCode.FAILED, message="", data=data))

def validate_error(errors: dict=None):
    if errors is None:
        error_message = "验证失败，出现未知错误"
    else:
        error_list = [f"{','.join(value)}" for key, value in errors.items()]
        error_message =', '.join(error_list)
    return jsonify(Response(code=HttpCode.VALIDATION_FAILED, message=error_message, data=errors))

def message(code:HttpCode=None, message: str=''):
    return jsonify(Response(code=code, message=message))


def success_message(message: str=''):
    return jsonify(Response(code=HttpCode.SUCCESS, message=message))
def failed_message(message: str=''):
    return jsonify(Response(code=HttpCode.FAILED, message=message))



def unauthorized_message(message: str=''):
    return jsonify(Response(code=HttpCode.UNAUTHORIZED, message=message))
def not_found_message(message: str=''):
    return jsonify(Response(code=HttpCode.NOT_FOUND, message=message))
def forbidden_message(message: str=''):
    return jsonify(Response(code=HttpCode.FORBIDDEN, message=message))